package cn.mastor.serve.service.impl;

import cn.mastor.basic.util.*;
import cn.mastor.order.domain.OrderProduct;
import cn.mastor.order.mapper.OrderProductMapper;
import cn.mastor.pet.domain.Pet;
import cn.mastor.pet.domain.PetOnlineAuditLog;
import cn.mastor.serve.domain.Product;
import cn.mastor.serve.domain.ProductOnlineAuditLog;
import cn.mastor.serve.mapper.ProductMapper;
import cn.mastor.serve.mapper.ProductOnlineAuditLogMapper;
import cn.mastor.serve.service.IProductService;
import cn.mastor.basic.service.impl.BaseServiceImpl;
import cn.mastor.user.domain.Logininfo;
import cn.mastor.user.domain.User;
import cn.mastor.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    OrderProductMapper orderProductMapper;
    @Override
    public Product getDetail(Long id) {
        return productMapper.getDetail(id);
    }
    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断，全部查询出来，只要状态是下架那么就不处理
//        for (int i = 0; i < ids.size(); i++) {
//            File file = new File("C:/Users/dscc/Desktop/bi前端2/pethome-web/department-web/detail/" + ids.get(i) + ".html");
//            file.delete();
//        }
        productMapper.batchOffsale(ids);
    }

    @Override
    public void takeOeder(Long id) {

    }

    @Override
    public AjaxResult bayServe(Long id, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByPhone(logininfo.getPhone());
        Product product = productMapper.findById(id);
        OrderProduct orderProduct = new OrderProduct();
//        BeanUtils.copyProperties(product,orderProduct);
        orderProduct.setUserId(user.getId());
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000));
        orderProduct.setState(0);
        orderProduct.setDigest(user.getUsername()+"购买了"+product.getName());
        product.setSalecount(product.getSalecount()+1);
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        orderProduct.setShopId(product.getShopId());
        orderProductMapper.add(orderProduct);
        return AjaxResult.me().success();
    }

    @Override
    public AjaxResult onsalePet(List<Long> ids) {
//        Map<Long, Pet> petMaps = petMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        Map<Long, Product> petMaps = new TreeMap<>();
        for (int i = 0; i < ids.size(); i++) {
            Product byId = productMapper.findById(ids.get(i));
            if (byId!=null){
                petMaps.put(byId.getId(), byId);
            }
        }

        for (Long id:ids){
            Product pet = petMaps.get(id);
            StringBuilder sb = new StringBuilder();
            if (pet != null){
                System.out.println(pet.getResources());
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    sb.append("服务上架百度AI审核名称失败，服务名为："+pet.getName());
                }
                if (pet.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])){
                        sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + pet.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())){
                        sb.append("服务上架百度AI审核图片失败，服务图片地址为：" + pet.getResources() + ";");
                    }
                }
                if (sb.length()>0){
                    ProductOnlineAuditLog petOnlineAuditLog = new ProductOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setProductId(id);
                    petOnlineAuditLog.setNote(sb.toString());
                    petOnlineAuditLog.setState(0);
                    productOnlineAuditLogMapper.add(petOnlineAuditLog);
                    return AjaxResult.me().fail("审核失败");
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                productMapper.update(pet);
//                System.out.println("--------------------下架测试-------------------------");
//                petMapper.update(pet);
//                try {
//                    FreemarkUtil.testHtml(id,path);
//                    System.out.println("-----------哈哈"+path);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        }return AjaxResult.me().success("审核成功");
    }
}
